package com.hsj.rabbitmq.controller;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import pojo.User;

/**
 * @author:Teacher黄
 * @date:Created at 2020/01/04
 */
@RestController
public class RabbitController {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 发送消息
     * @return
     */
    @GetMapping("/mq/send/{msg}")
    public String send(@PathVariable String msg){
        rabbitTemplate.convertAndSend("hsj_exchange","hsj_route","Hello "+msg);
        return "success";
    }

    @GetMapping("/mq/obj/{name}")
    public String sendObj(@PathVariable String name){
        User user = new User();
        user.setUserName(name);
        rabbitTemplate.convertAndSend("hsj_exchange","object_route",user);
        return "success";
    }


    @GetMapping("/mq/pub/{name}")
    public String sendPub(@PathVariable String name){
        rabbitTemplate.convertAndSend("pub_exchange","pub_sub",name);
        return "success";
    }


    @GetMapping("/mq/reject/{name}")
    public String sendRej(@PathVariable String name){
        rabbitTemplate.convertAndSend("reject_exchange","reject.hsj",name);
        return "success";
    }


    @GetMapping("/mq/recover/{name}")
    public String sendRec(@PathVariable String name){
        rabbitTemplate.convertAndSend("recover_exchange","recover",name);
        return "success";
    }

    @GetMapping("/mq/confirm/{name}")
    public String sendConfirm(@PathVariable String name){
        // 返回的消息
        Message returnMsg = new Message(name.getBytes(),null);
        CorrelationData correlationData = new CorrelationData();
        correlationData.setReturnedMessage(returnMsg);
        rabbitTemplate.convertAndSend("confirm_exchange","confirm",name,correlationData);
        return "success";
    }


    @GetMapping("/mq/return/{name}")
    public String sendReturn(@PathVariable String name){
        // 发送的消息
        User user = new User();
        user.setUserName(name);
        user.setId(1111);
        user.setUserAge(100);
        // 返回的消息
        Message returnMsg = new Message(name.getBytes(),null);
        CorrelationData correlationData = new CorrelationData();
        correlationData.setReturnedMessage(returnMsg);
        rabbitTemplate.convertAndSend("return_exchange","confirm",user,correlationData);
        return "success";
    }
}
